<template>
  <div class="warpper-box">
    <!-- <enterprise-select v-if="userInfo.type == 4" ref="enterprise" @enterpriseChange="enterpriseChange"></enterprise-select> -->
    <a-card style="margin-top: 10px;">
      <div style="display: flex;justify-content: flex-start;align-items: flex-start;">
        <form-create v-model="params.fApi" :rule="params.rule" :option="params.option" @btn-search-click="loadTable(true)"
          @btn-reset-click="reset" />
        <span style="padding: 5px 0 0 0;">
          <a-button type="primary" icon="plus" @click="openPlan">新增</a-button>
        </span>
      </div>
      <a-table :columns="table.columns" :data-source="table.loadData" :pagination="table.pagination"
        @change="handleTableChange" rowKey="caseId">
        <!-- 操作栏-->
        <span slot="action" slot-scope="text, record">
          <!-- 编辑模块 | 字段 -->
          <a @click="planHandle(record)">方案配置</a>
          <a-divider type="vertical" ></a-divider>
          <a @click="$refs.enterpriseAll.openAdmin(record)">企业选择</a>
          <a-divider type="vertical" />
          <a-popconfirm title="确认删除?" ok-text="确认" cancel-text="取消" @confirm="remove(record)">
            <a>删除</a>
          </a-popconfirm>
        </span>
      </a-table>
    </a-card>
    <!-- 参保方案 -->
    <a-modal v-model="isPlan" title="方案添加" :confirmLoading="submitLoading" width="500px" ok-text="确认" cancel-text="取消" @ok="resolveHandle">
      <a-space style="padding-left: 80px;">
        方案名称：
        <a-input placeholder="请输入方案名称" v-model="caseName" />
      </a-space>
      <a-space style="padding-left: 24px;padding-top: 10px;">
        城市方案模板选择：
        <a-cascader :options="cityJson" placeholder="请选择" v-model="city" style="width: 240px;"/>
      </a-space>
    </a-modal>
    <!-- 企业选择 -->
    <enterprise-all-select ref="enterpriseAll"></enterprise-all-select>
  </div>
</template>

<script>
import store from '@/store'
import EnterpriseSelect from "@/components/human/enterpriseSelect.vue"
import EnterpriseAllSelect from "@/components/human/enterpriseAllSelect.vue"
import {
  PARAMS_PUBLICPLAN,
  COLUMNS_PUBLICPLAN
} from "./form"
import {
  OPTION_BLOCK,
  OPTION_INLINE
} from "../base_form"
import {
  cityJson
} from "@/api/human/city.js"
import {
  getTablePage,
  caseAdd,
  caseDelete
} from "@/api/human/ensurePublicPlan"
import { mapGetters, mapActions } from 'vuex'
export default {
  name: 'publicPlan',
  components: {
    EnterpriseSelect,
    EnterpriseAllSelect
  },
  data() {
    return {
      params: {
        fApi: {},
        rule: PARAMS_PUBLICPLAN,
        option: OPTION_INLINE,
      },
      cityJson: cityJson,
      table: {
        columns: COLUMNS_PUBLICPLAN,
        loadData: [],
        pagination: {
          current: 1,
          pageSize: 10,
          total: 0,
          showSizeChanger: true,
					showQuickJumper: true,
          pageSizeOptions: ["10", "20", "50", "100"],
          showTotal: total => `共有 ${total} 条数据`,
        }
      },
      pagination: { current: 1, pageSize: 10 },

      enterpriseId: "", // 企业id
      enterpriseData: [], // 客户列表

      submitLoading: false,
      isPlan: false,
      caseName: '',
      city: [],
    }
  },
  filters: {
    filterMore(value, num) {
      if (!value) return ''
      if (value.length > num) {
        return value.slice(0, num) + '...'
      }
      return value
    }
  },
  mounted() {
    window.addEventListener('keyup', this.keyDown);
    this.loadTable(true);
  },
  destroyed() {
    window.removeEventListener('keyup', this.keyDown);
  },
  beforeCreate() {
    this.events_form = this.$form.createForm(this, {
      name: 'events_form'
    })
  },
  computed: {
    ...mapGetters(['userInfo']),
  },
  created() {

  },
  methods: {
    // 获取列表
    async loadTable(flag) {
      let params = this.params.fApi.formData();
      if (flag) {
        this.table.pagination.current = 1;
        this.table.pagination.pageSize = 10;
      }
      params.enterpriseId = this.enterpriseId;
      params.pageNum = this.table.pagination.current;
      params.pageSize = this.table.pagination.pageSize;
      const { code, data } = await getTablePage(params);
      if (code === '1') {
        this.table.loadData = data.content;
        this.table.pagination.total = data.total || 0;
      }
    },
    handleTableChange(pagination) {
      this.table.pagination.current = pagination.current;
      this.table.pagination.pageSize = pagination.pageSize;
      this.loadTable(false);
    },
    // 删除方案
    async remove(row) {
      let params =  {
        caseId: row.caseId,
      }
      const { code } = await caseDelete(params)
      if (code == 1) {
        this.$message.success('操作成功');
        this.loadTable(true);
      }
    },
    // 打开方案弹窗
    openPlan() {
      this.isPlan = true;
      this.caseName = "";
      this.city = [];
    },
    // 添加方案
    async resolveHandle() {
      if (this.caseName == "") {
        return this.$message.warning('请输入方案名称')
      }
      if (this.city.length == 0) {
        return this.$message.warning('请选择城市')
      }
      this.city.pop();
      let params =  {
        enterpriseId: this.enterpriseId,
        caseName: this.caseName,
        city: this.city.join(',')
      }
      this.submitLoading = true;
      const { code } = await caseAdd(params)
      if (code == 1) {
        this.isPlan = false;
        this.submitLoading = false;
        this.$message.success('操作成功');
        this.loadTable(true);
      }
    },
    // 方案配置
    planHandle(row) {
      this.$router.push({ name: 'humanResourcesEnsurePublicPlanHandleCode', query: { caseId: row.caseId }});
    },
    // 企业选择回调
    enterpriseChange(value) {
      this.enterpriseId = value;
      this.loadTable(true);
    },
    addLoad() {
      this.loadTable(true);
    },
    reset() {
      this.params.fApi.resetFields();
      this.loadTable(true);
    }
  }
}
</script>
<style lang="less"></style>